<?php
class Babel {
	/**
	 * 生成图片
	 * */
	public function mol2gif($id,$mol,$subpath=""){
		$basepath="/data/www/smartchem/public/svg/".$subpath;
		$mol = str_replace("Untitled Document","",str_replace("{#n#}", "\r\n", $mol));
		file_put_contents($basepath.$id.".sdf", $mol);
		system("babel ".$basepath.$id.".sdf -O ".$basepath.$id.".svg");
		system("sed -i \"s/stroke-width=\\\"1\\\"/stroke-width=\\\"0.5\\\"/g\" ".$basepath.$id.".svg");
		$svgin = file_get_contents($basepath.$id.".svg");
		$frame = new Imagick();
		$frame->readImageBlob($svgin);
		$frame->setImageFormat("gif");
		if($frame->getImageWidth() >380||$frame->getImageHeight() >400)
			$frame->scaleImage(380,400,true);
		$frame->writeImage($basepath."big/".$id.'.gif');
		$frame->scaleImage(200,120,true);
		$frame->writeImage($basepath.$id.'.gif');
		//$frame->scaleImage(450,350,true);
		//$frame->writeImage($basepath."big/".$id.'.gif');
	}
	/**
	 * 反应式转gif
	 * */
	public function rxn2gif($id,$rxn){
		if($rxn=="")
			return;
		//echo $rxn;exit;
		$basepath="/data/www/smartchem/public/svg/";
		$rxn=str_replace("{#n#}", "\r\n", $rxn);
		$sdfs = explode('$MOL'."\r\n",$rxn);
		$header =trim(str_replace('$RXN','',$sdfs[0]));
		$headers = explode('  ',$header);
		$prev = $headers[0];
		if(intval($prev)==0)return;
		$n = count($sdfs);
		$imgn = $n*2-1;
		$aniGif = new Imagick();
		$aniGif->setFormat("gif");
		for($i=1;$i < $n;$i++){
			//echo $basepath.$id."_".$i.".sdf";
			file_put_contents($basepath.$id."_".$i.".sdf",$sdfs[$i]);
			system("babel ".$basepath.$id."_".$i.".sdf -isdf -O ".$basepath.$id."_$i.svg");
			system("sed -i \"s/stroke-width=\\\"1\\\"/stroke-width=\\\"0.5\\\"/g\" ".$basepath.$id."_$i.svg");
			$svgin = file_get_contents($basepath.$id."_".$i.".svg");
			$frame = new Imagick();
			$frame->readImageBlob($svgin);
			$frame->setImageFormat("gif");
			$frame->setImageBorderColor("#ffffff");
			$aniGif->addImage($frame);
			if($i==$prev){
				$frame1 = new Imagick($basepath."to.gif");
				$frame1->setImageFormat("gif");
				$frame1->setImageBorderColor("#ffffff");
				$aniGif->addImage($frame1);
			}
			elseif($i+1<$n){
				$frame1 = new Imagick($basepath."plus.gif");
				$frame1->setImageFormat("gif");
				$frame1->setImageBorderColor("#ffffff");
				$aniGif->addImage($frame1);
			}
		}
		$aniGif->setBackgroundColor("#ffffff");
		$imdr = new imagickdraw();
		$imdr->color(0,0,0);
		$aniGif = $aniGif->montageImage($imdr,$imgn."x1+0+0","",imagick::MONTAGEMODE_CONCATENATE,"5x5+0");
		$aniGif->setImageExtent($aniGif->getImageWidth()*$n/(2*$n-1)+($n-1)*60,$aniGif->getImageHeight());
		$aniGif->scaleImage(600,400,true);
		$aniGif->writeImage($basepath.$id.'.route.gif');
	}
	/**
	 * 格式转换
	 * */
	public function molecule_to_smiles($mol){
		$mol = str_replace("{#n#}", "\r\n", $mol);
		$t = time();
		$tmpsdf = "/tmp/".$t.".sdf";
		$tmpsmi = "/tmp/".$t.".smi";
		$tmpsmi_1 =  "/tmp/".$t."_1.smi";
		file_put_contents($tmpsdf, $mol);
		system("babel $tmpsdf $tmpsmi");
		$cmd = "cat /tmp/".$t.".smi|awk '{print $1}'>".$tmpsmi_1;
		system($cmd);
		$smi = file_get_contents($tmpsmi_1);		
		unlink($tmpsdf);
		unlink($tmpsmi);
		unlink($tmpsmi_1);
		return $smi;
	}
	public function molecule_to_inchi($mol){
		$mol = str_replace("{#n#}", "\r\n", $mol);
		$t = time();
		$tmpsdf = "/tmp/".$t.".sdf";
		$tmpinchi = "/tmp/".$t.".inchi";
		$tmpinchi_1 =  "/tmp/".$t."_1.inchi";
		file_put_contents($tmpsdf, $mol);
		system("babel $tmpsdf $tmpinchi");
		$cmd = "cat /tmp/".$t.".inchi|awk '{print $0}'>".$tmpinchi_1;
		system($cmd);
		$inchi = file_get_contents($tmpinchi_1);
		unlink($tmpsdf);
		unlink($tmpinchi);
		unlink($tmpinchi_1);
		return trim($inchi);
	}
	public function smile_to_prop($smi){
		$mol = $this->smile_to_molecule($smi);
		return $this->molecule_to_prop($mol);
	}
	public function smile_to_molecule($smi){
		//$mol = str_replace("{#n#}", "\r\n", $mol);
		$t = time();
		$tmpsmi = "/tmp/".$t.".smi";
		$tmpsdf = "/tmp/".$t.".sdf";		
		file_put_contents($tmpsmi, $smi);
		system("babel $tmpsmi $tmpsdf");
		$r = file_get_contents($tmpsdf);
		unlink($tmpsmi);
		unlink($tmpsdf);
		return $r;
	}
	public function molecule_to_prop($mol){
		$mol = str_replace("{#n#}", "\r\n", $mol);
		$t = time();
		$tmpsdf = "/tmp/".$t.".sdf";
		$tmpprop = "/tmp/".$t.".prop";
		file_put_contents($tmpsdf, $mol);
		system("obprop $tmpsdf > $tmpprop");
		$txt = file_get_contents($tmpprop);
		$lines = explode("\n", $txt);
		$result = array();
		foreach($lines as $line){
			$line = preg_replace('/\s+/', ' ', $line);
			$prop = explode(" ",$line);
			if(count($prop)>1){
				$result[$prop[0]] = $prop[1];
			}
		}
		$mol = str_replace("\r\n", "{#n#}", $mol);
		$mol = str_replace("Untitled Document", "", $mol);
		$mol = str_replace("ChemDraw", "SmartChem", $mol);
		$result["mol"] = $mol; 
		unlink($tmpsdf);
		unlink($tmpprop);
		return $result;
	}
	public function smile_to_inchi($smile){
		$t = time();
		$tmpsmi = "/tmp/".$t.".smi";
		$tmpinchi = "/tmp/".$t.".inchi";
		$tmpinchi_1 =  "/tmp/".$t."_1.inchi";
		file_put_contents($tmpsmi, $smile);
		system("babel $tmpsmi $tmpinchi");
		$cmd = "cat /tmp/".$t.".inchi|awk '{print $0}'>".$tmpinchi_1;
		system($cmd);
		$inchi = file_get_contents($tmpinchi_1);
		unlink($tmpsmi);
		unlink($tmpinchi);
		unlink($tmpinchi_1);
		return $inchi;
	}
	/**
	 * @fs 索引文件
	 * @smi 检索条件
	 * @stype 检索类型3精确2相似1子结构
	 * @x 相似度
	 * @result0
	 * */
	public function getIdListbySmi($fs,$smi,$stype,$x,$result0){
		$result = array();
		/*$stype = $r["stype"];//检索类型
		$smi = $this->GetSmile($r["mol"]);//smile
		$x = $r["x"];//相似度*/
		$t = time();
		if($x>=1)$x=0.9999;
// 		if($stype == "3"){
// 			$x = 0.9999;
// 		}
		if( $stype == "2" ){//相似度检索
			$tmpfile = "/tmp/".$t."_sim.smi";
			system("babel $fs -s'$smi' $tmpfile -at$x");
			$cmd = "cat /tmp/".$t."_sim.smi|awk '{print $2}'>/tmp/".$t."_sim1.smi";
			system("$cmd");
			$result = file("/tmp/".$t."_sim1.smi");			
		}else if( $stype == "1" ){//子结构
			$tmpfile = "/tmp/".$t."_sub.smi";
			system("babel $fs -s'$smi' $tmpfile -xt");
			$result = file("/tmp/".$t."_sub.smi");
		}
		unlink("/tmp/".$t."_sub.smi");
		unlink("/tmp/".$t."_sim.smi");
		unlink("/tmp/".$t."_sim1.smi");
		return $result;
	}
}